package com.yunche.yunchebusiness.mvp.presenter

import com.yunche.yunchebusiness.base.mvp.BasePresenter
import com.yunche.yunchebusiness.mvp.contract.EvaluateContract
import com.yunche.yunchebusiness.mvp.model.EvaluateModel
import com.yunche.yunchebusiness.network.exception.ErrorStatus
import com.yunche.yunchebusiness.network.exception.ExceptionHandle
import okhttp3.RequestBody

/**
 * Create by Crcker on 2019/1/2 17:04
 * Email: Crcker@163.com
 */


class EvaluatePresenter : BasePresenter<EvaluateContract.View>(), EvaluateContract.Presenter {
    private val model by lazy {
        EvaluateModel()
    }

    override fun saveEvaluate(requestBody: RequestBody) {
        mRootView?.showLoading()
        var disposable = model.saveEvaluate(requestBody).subscribe({
            mRootView!!.apply {
                hideLoading()
                if(it.resultCode == ErrorStatus.SUCCESS){
                    saveEvaluateSuccess()
                }else{
                    showError(it.message!!)
                }
            }
        }, {
            mRootView?.hideLoading()
            mRootView?.showError(ExceptionHandle.handleException(it))
        })

        addSubscription(disposable)
    }

    override fun getEvaluate(requestBody: RequestBody) {
        mRootView?.showLoading()
        var disposable = model.getEvaluate(requestBody).subscribe({
            mRootView!!.apply {
                hideLoading()
                if (it.resultCode == ErrorStatus.SUCCESS) {
                    getEvaluateSuccess(it.datas!!)
                } else {
                    showError(it.message!!)
                }
            }
        }, {
            mRootView?.hideLoading()
            mRootView?.showError(ExceptionHandle.handleException(it))
        })

        addSubscription(disposable)
    }

    override fun getColor(modelId: String) {
        mRootView?.showLoading()
        var disposable = model.getColor(modelId).subscribe({
            mRootView!!.apply {
                hideLoading()
                if (it.resultCode == ErrorStatus.SUCCESS) {
                    getColorSuccess(it.datas!!)
                } else {
                    showError(it.message!!)
                }
            }
        }, {
            mRootView?.hideLoading()
            mRootView?.showError(ExceptionHandle.handleException(it))
        })

        addSubscription(disposable)
    }

    override fun getArea() {
        mRootView?.showLoading()
        var disposable = model.getArea().subscribe({
            mRootView!!.apply {
                hideLoading()
                if (it.resultCode == ErrorStatus.SUCCESS) {
                    getAreaSuccess(it.datas!!)
                } else {
                    showError(it.message!!)
                }
            }
        }, {
            mRootView?.hideLoading()
            mRootView?.showError(ExceptionHandle.handleException(it))
        })
        addSubscription(disposable)
    }
}